package com.bcsp.xml.ex1;

import com.bcsp.xml.Test4;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class StudentDao {

    String filename = "b.xml";
    Document document;
    String path;
    {
        //读取xml内容，并转化为List<Student>
        //使用dom4j
        //获取文件路径
        path = Test4.class.getClassLoader().getResource(filename).getPath();
        //准备SAXReader
        SAXReader reader = new SAXReader();
        //读取文档
        try {
            document = reader.read(new File(path));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        System.out.println("初始化document");
    }


    public List<Student> getAll() throws Exception{
        List<Student> students = new ArrayList<>();
        //获取根节点
        Element root = document.getRootElement();
        //获取子元素节点
        List<Element> stusElement = root.elements("student");
        for (Element stuElement : stusElement){
            String id = stuElement.attributeValue("ID");
            String name = stuElement.elementText("name");
            String age = stuElement.elementText("age");
            String gender = stuElement.elementText("gender");
            Student student = new Student();
            student.setId(Integer.parseInt(id));
            student.setName(name);
            student.setAge(Integer.parseInt(age));
            student.setGender(gender);
            students.add(student);
        }
        return students;
    }

    public Student getOne(int id){
        Element stuElement = this.document.elementByID(String.valueOf(id));
        if(stuElement == null){
            return null;
        }
        String name = stuElement.elementText("name");
        String age = stuElement.elementText("age");
        String gender = stuElement.elementText("gender");
        Student student = new Student();
        student.setId(id);
        student.setName(name);
        student.setAge(Integer.parseInt(age));
        student.setGender(gender);
        return student;
    }

    public void add(Student student){
        //获取根节点
        Element root = document.getRootElement();

        Element studentElement = root.addElement("student");
        //添加属性
        studentElement.addAttribute("ID",String.valueOf(student.getId()));
        //添加子节点
        Element nameElement = studentElement.addElement("name");
        nameElement.setText(student.getName());
        Element ageElement = studentElement.addElement("age");
        ageElement.setText(String.valueOf(student.getAge()));
        Element genderElement = studentElement.addElement("gender");
        genderElement.setText(student.getGender());

        saveXml();
    }

    public void update(Student student){
        //获取根节点
        Element root = document.getRootElement();
        //根据id去查找元素
        Element el = root.elementByID(String.valueOf(student.getId()));
        el.element("age").setText(String.valueOf(student.getAge()));
        el.element("gender").setText(student.getGender());
        el.element("name").setText(student.getName());

        saveXml();
    }

    public void delete(int id){
        //获取根节点
        Element root = document.getRootElement();
        //删除索引为1的元素
        Element el = root.elementByID(String.valueOf(id));
        root.remove(el);

        saveXml();
    }

    private void saveXml(){
        //格式化
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        //xml输出流
        XMLWriter writer = null;
        try {
            writer = new XMLWriter(new FileWriter(path),format);
            //输出
            writer.write(document);
            //关闭流
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
